package zym.diandian.module.volume;


import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import zym.diandian.R;

//音量设置
public class VolumeFrame extends Fragment {
    private ListView list_view;
    private VolumeAdapter adapter;

    private AudioManager mAudioManager;

    private final SparseArray<SeekBar> seekBars = new SparseArray<SeekBar>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.volume_fragment, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        list_view = (ListView) view.findViewById(R.id.list_view);

        this.mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
        this.mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
        List<HashMap<String, Object>> mData = new ArrayList<HashMap<String, Object>>();
        //---------------------------------------------------语音电话
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("volume", this.mAudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL));
        map.put("volumeMax", this.mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL));
        map.put("volumeType", AudioManager.STREAM_VOICE_CALL);
        map.put("volumeName", "通话");
        mData.add(map);
        //---------------------------------------------------系统
        HashMap<String, Object> mapa = new HashMap<String, Object>();
        mapa.put("volume", this.mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM));
        mapa.put("volumeMax", this.mAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM));
        mapa.put("volumeType", AudioManager.STREAM_SYSTEM);
        mapa.put("volumeName", "系统");
        mData.add(mapa);
        //---------------------------------------------------铃声
        HashMap<String, Object> mapb = new HashMap<String, Object>();
        mapb.put("volume", this.mAudioManager.getStreamVolume(AudioManager.STREAM_RING));
        mapb.put("volumeMax", this.mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING));
        mapb.put("volumeType", AudioManager.STREAM_RING);
        mapb.put("volumeName", "铃声");
        mData.add(mapb);
        //---------------------------------------------------音乐
        HashMap<String, Object> mapc = new HashMap<String, Object>();
        mapc.put("volume", this.mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
        mapc.put("volumeMax", this.mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
        mapc.put("volumeType", AudioManager.STREAM_MUSIC);
        mapc.put("volumeName", "媒体");
        mData.add(mapc);
        //---------------------------------------------------闹钟
        HashMap<String, Object> mapd = new HashMap<String, Object>();
        mapd.put("volume", this.mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM));
        mapd.put("volumeMax", this.mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM));
        mapd.put("volumeType", AudioManager.STREAM_ALARM);
        mapd.put("volumeName", "闹钟");
        mData.add(mapd);
        //---------------------------------------------------通知
        HashMap<String, Object> mape = new HashMap<String, Object>();
        mape.put("volume", this.mAudioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION));
        mape.put("volumeMax", this.mAudioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION));
        mape.put("volumeType", AudioManager.STREAM_NOTIFICATION);
        mape.put("volumeName", "通知");
        mData.add(mape);
        //---------------------------------------------------拨号音
        HashMap<String, Object> mapf = new HashMap<String, Object>();
        mapf.put("volume", this.mAudioManager.getStreamVolume(AudioManager.STREAM_DTMF));
        mapf.put("volumeMax", this.mAudioManager.getStreamMaxVolume(AudioManager.STREAM_DTMF));
        mapf.put("volumeType", AudioManager.STREAM_DTMF);
        mapf.put("volumeName", "拨号按键音");
        mData.add(mapf);
        //---------------------------------------------------
        this.adapter = new VolumeAdapter(mData);
        this.list_view.setAdapter(this.adapter);
    }


    private class VolumeAdapter extends BaseAdapter {

        private final List<HashMap<String, Object>> mList;


        VolumeAdapter(List<HashMap<String, Object>> mData) {
            this.mList = mData;
        }

        @Override
        public int getCount() {
            return this.mList.size();
        }

        @Override
        public Object getItem(int position) {
            return this.mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if (null == convertView) {
                viewHolder = new ViewHolder();

                convertView = View.inflate(getActivity(), R.layout.volume_list_item, null);
                viewHolder.t = (TextView) convertView.findViewById(R.id.volume_item_tv);
                viewHolder.s = (SeekBar) convertView.findViewById(R.id.volume_item_seekbar);

                seekBars.put(position, viewHolder.s);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.t.setText("" + this.mList.get(position).get("volumeName"));
            viewHolder.s.setMax((Integer) this.mList.get(position).get("volumeMax"));
            viewHolder.s.setProgress((Integer) this.mList.get(position).get("volume"));
            viewHolder.s.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    mAudioManager.setStreamVolume((Integer) VolumeAdapter.this.mList.get(position).get("volumeType"), viewHolder.s.getProgress(), 1);
                }
            });

            return convertView;
        }
    }

    private static class ViewHolder {

        SeekBar s;

        TextView t;
    }


}
